$(function () {
    $("#jqGrid").jqGrid({
        url: '../posts/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true, align: "center"},
            // {label: '用户id', name: 'userId', index: 'user_id', width: 80, align: "center"},
            // {label: '发帖ip', name: 'ip', index: 'ip', width: 80, align: "center"},
            // {label: '城市', name: 'city', index: 'city', width: 80, align: "center"},
            {label: '标题', name: 'title', index: 'title', width: 180, align: "center"},
            {label: '阅读数', name: 'viewCount', index: 'view_count', width: 80, align: "center"},
            {label: '点赞数量', name: 'likeCount', index: 'like_count', width: 80, align: "center"},
            // {label: '内容', name: 'contentDesc', index: 'content_desc', width: 80, align: "center"},
            {
                label: '是否置顶',
                name: 'isTop',
                index: 'is_top',
                width: 80,
                align: "center",
                formatter: function (value) {
                    if (value === 0) {
                        return '<span class="label label-default">申请</span>';
                    } else if (value === 1) {
                        return '<span class="label label-success">置顶</span>';
                    } else if (value === 2) {
                        return '<span class="label label-cyan">不置顶</span>';
                    } else {
                        return '<span class="label label-danger">未知</span>';
                    }
                }
            },
            {
                label: '是否推荐', name: 'isRecommend', index: 'is_recommend', width: 80, align: "center",
                formatter: function (value) {
                    if (value === 0) {
                        return '<span class="label label-danger">否</span>';
                    } else if (value === 1) {
                        return '<span class="label label-success">是</span>';
                    } else {
                        return '<span class="label label-danger">未知</span>';
                    }
                }
            },
            {
                label: '是否热门', name: 'isHot', index: 'is_hot', width: 80, align: "center",
                formatter: function (value) {
                    if (value === 0) {
                        return '<span class="label label-danger">否</span>';
                    } else if (value === 1) {
                        return '<span class="label label-success">是</span>';
                    } else {
                        return '<span class="label label-danger">未知</span>';
                    }
                }
            },
            {
                label: '是否显示', name: 'status', index: 'status', width: 80, align: "center",
                formatter: function (value) {
                    if (value === 0) {
                        return '<span class="label label-danger">否</span>';
                    } else if (value === 1) {
                        return '<span class="label label-success">是</span>';
                    } else {
                        return '<span class="label label-danger">未知</span>';
                    }
                }
            },
            {
                label: '发布时间', name: 'addTime', index: 'add_time', width: 80, align: "center",
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            }],
        viewrecords: true,
        height: 495,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        posts: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        },
        isUpload: 0,
        uploadList: [],
        imgName: '',
        visible: false,
        isShow: false
    },
    mounted() {
        this.uploadList = this.$refs.upload.fileList;

    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.posts = {
                isTop: 1,
                isRecommend: 0,
                isHot: 0,
                status: 1
            };
            vm.isUpload = 1;
            vm.uploadList = [];
            vm.isUploadChange();
        },
        update: function (event) {
            let id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.uploadList = [];
            vm.getInfo(id);
            vm.getGoodsGallery(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.posts.id == null ? "../posts/save" : "../posts/update";
            vm.posts.postImgList = vm.uploadList;
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(vm.posts),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../posts/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../posts/info/" + id, function (r) {
                vm.posts = r.posts;
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleRemove(file) {
            // 从 upload 实例删除数据
            const fileList = this.uploadList;
            this.uploadList.splice(fileList.indexOf(file), 1);
        },
        handleSuccess(res, file) {
            // 因为上传过程为实例，这里模拟添加 url
            file.picUrl = res.url;
            file.name = res.url;
            vm.uploadList.add(file);
        },
        handleBeforeUpload() {
            const check = this.uploadList.length < 9;
            if (!check) {
                this.$Notice.warning({
                    title: '最多只能上传 9 张图片。'
                });
            }
            return check;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },

        openComment: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            openWindow({
                type: 2,
                title: '评论列表',
                content: '../shop/postscomment.html?postId=' + id
            });
        },
        getGoodsGallery: function (id) {//获取图片列表
            $.get("../postspicture/queryAll?postId=" + id, function (r) {
                vm.uploadList = r.list;
                if (vm.uploadList.length > 0) {
                    vm.isUpload = 1;
                    vm.isUploadChange();
                }
            });
        },
        handleView(name) {
            this.imgName = name;
            this.visible = true;
        },
        isUploadChange() {
            let isUpload = vm.isUpload;
            if (isUpload === 1 || isUpload === '1') {
                vm.isShow = true;
            } else {
                vm.isShow = false;
            }
        }
    }
});